package com.cskaoyan.controller.admin;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/11/29 18:24
 */

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;
import com.cskaoyan.service.admin.UserService;

import com.cskaoyan.utils.UserControllerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2021/11/29 15:11
 */

@RestController
public class UserController {

    @Autowired
    UserService userService;

    //例子开头
    ///admin/user/list?page=1&limit=20&username=user&mobile=110&sort=add_time&order=desc
    @RequestMapping("admin/user/list")
    public BaseRespVo list(String username, String mobile, BaseParam param) {
        BaseData data = userService.list(username, mobile, param);
        return BaseRespVo.ok(data);
    }

    //例子结尾
    //根据id查找用户
    @RequestMapping("admin/user/detail")
    public BaseRespVo id(String id) {
        //调用了工具类
        UserControllerUtils userControllerUtils = new UserControllerUtils();
        if (userControllerUtils.userIdOfLegal(id) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        MarketUser data = userService.userOfID(id);
        return BaseRespVo.ok(data);
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 10:38
     * @Description： 更改会员管理的详情
     */
    @RequestMapping(value = "admin/user/update", method = RequestMethod.POST)
    public BaseRespVo update(@RequestBody MarketUser user) {
        int data = userService.update(user);
        return BaseRespVo.ok(data);
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 11:09
     * @Description： 收货地址的显示与查询
     */
    @RequestMapping("admin/address/list")
    public BaseRespVo address(BaseParam param, String name, String userId) {
        String regex = "^[0-9]*$";
        if (userId != null && userId != "") {
            boolean flag = userId.matches(regex);
            if (!flag) {
                return BaseRespVo.fail(402, "参数值不对");
            }
        }
        BaseData data = userService.addressList(param, name, userId);
        return BaseRespVo.ok(data);
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 14:56
     * @Description： 会员收藏的显示与查询
     */

    @RequestMapping("admin/collect/list")
    public BaseRespVo collect(BaseParam param, String userId, String valueId) {
        //调用了工具类
        UserControllerUtils userControllerUtils = new UserControllerUtils();
        if (userControllerUtils.userIdOfLegal(userId) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        if (userControllerUtils.userIdOfLegal(valueId) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }

        BaseData data = userService.collectList(param, userId, valueId);
        return BaseRespVo.ok(data);
    }


    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 15:20
     * @Description： 会员足迹
     */

    @RequestMapping("admin/footprint/list")
    public BaseRespVo footprint(BaseParam param, String userId, String goodsId) {
        //调用了工具类
        UserControllerUtils userControllerUtils = new UserControllerUtils();
        if (userControllerUtils.userIdOfLegal(userId) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        if (userControllerUtils.userIdOfLegal(goodsId) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        BaseData data = userService.footprintList(param, userId, goodsId);
        return BaseRespVo.ok(data);
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 15:37
     * @Description： 搜索历史
     */
    @RequestMapping("admin/history/list")
    public BaseRespVo history(BaseParam param, String userId, String keyword) {
        UserControllerUtils userControllerUtils = new UserControllerUtils();
        if (userControllerUtils.userIdOfLegal(userId) == 0) {
            return BaseRespVo.fail(402, "参数值不对");
        }
        BaseData data = userService.historyList(param, userId, keyword);
        return BaseRespVo.ok(data);
    }

    /**
     * @Author ChenZhuqi
     * @Date 2021/11/30 15:55
     * @Description： 意见反馈
     */
    @RequestMapping("admin/feedback/list")
    public BaseRespVo feedback(BaseParam param, String id, String username) {
        BaseData data = userService.feedbackList(param, id, username);
        return BaseRespVo.ok(data);
    }

}

